#!/bin/bash

get_current_path() {
  echo $(realpath $(dirname "$0"))
}

path::is_file() {
  path=$1
  if [ -f $path ]; then
    return 0
  fi

  return 1
}

path::is_dir() {
  path=$1
  if [ -d $path ]; then
    return 0
  fi

  return 1
}

path::basename() {
  local path=$1

  if [ "$path" = "" ]; then
    log::error "path is required"
    exit 1
  fi

  echo $(string::split_last_element '/' $path)
}

path::dirname() {
  local path=$1

  if [ "$path" = "" ]; then
    log::error "path is required"
    exit 1
  fi

  echo "/$(string_split_slice '/' $path 0 -1)"
}

path::join() {
  local str=$(array::join '/' $@)
  # echo $str
  echo $(string::replace $str '/\{2,\}' '/')
}

realpath() {
  OURPWD=$PWD
  cd "$(dirname "$1")"
  LINK=$(readlink "$(basename "$1")")
  while [ "$LINK" ]; do
    cd "$(dirname "$LINK")"
    LINK=$(readlink "$(basename "$1")")
  done
  REALPATH="$PWD/$(basename "$1")"
  cd "$OURPWD"
  echo "$REALPATH"
}


export -f get_current_path
export -f path::is_file
export -f path::is_dir

export -f path::basename
export -f path::dirname
export -f path::join

export -f realpath
